package com.airbus.ajcp.trigger.converterV3;

import com.airbus.ajcp.server.production.output.MessageLevel;
import com.airbus.ajcp.server.trigger.api.TriggerApi;
import com.airbus.ajcp.server.xml.jaxp.WorkPackageDOM;
import com.airbus.ajcp.trigger.triggerBundle.Constant;
import com.airbus.ajcp.trigger.triggerBundle.GeneralValidator;
import com.airbus.ajcp.trigger.triggerBundle.XPathEx;
import com.sun.org.apache.xerces.internal.parsers.DOMParser;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Properties;
import javax.xml.datatype.XMLGregorianCalendar;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GenericConverter
{
  private TriggerApi api = null;
  private String input;
  private String output;
  private Properties env_var_prop = null;

  private Properties publication_prop = null;
  private Document documentV2V3;
  private Document documentV3;
  private ArrayList<Publication> listPubli;
  private String envFile;

  public GenericConverter(TriggerApi api, String input, String output)
  {
    this.api = api;
    this.input = input;
    this.output = output;
    try
    {
      this.env_var_prop = new Properties();
      FileInputStream in = new FileInputStream(api.getProdParamSetPath().getPath() + File.separator + Constant.conversionPropertiesRep + File.separator + Constant.envVarV2File);
      this.env_var_prop.load(in);
      in.close();
    }
    catch (Exception e)
    {
      GeneralValidator.logError(api, e);
    }

    try
    {
      this.publication_prop = new Properties();
      FileInputStream in = new FileInputStream(api.getProdParamSetPath().getPath() + File.separator + Constant.conversionPropertiesRep + File.separator + Constant.publicationV2File);
      this.publication_prop.load(in);
      in.close();
    }
    catch (Exception e)
    {
      GeneralValidator.logError(api, e);
    }
  }

  public Document convert()
  {
    try
    {
      DOMParser parser = new DOMParser();
      parser.parse(this.input);
      this.documentV2V3 = parser.getDocument();
      this.documentV3 = ((Document)this.documentV2V3.cloneNode(true));

      XPathEx.createFileFromFileWithDocType(this.documentV2V3, this.output);
      DOMParser parserV3 = new DOMParser();
      parserV3.parse(this.output);
      this.documentV3 = parserV3.getDocument();

      Element ajcpPubNodeV2V3 = (Element)XPathEx.getNode(Constant.AJCPPUB, this.documentV2V3);
      Element ajcpPubNodeV3 = (Element)XPathEx.getNode(Constant.AJCPPUB, this.documentV3);
      if (ajcpPubNodeV2V3 != null)
      {
        createWorkpackage(ajcpPubNodeV3, ajcpPubNodeV2V3);
      }

      String wpfPath = this.api.getWpfPath();
      String v3File = Constant.EMPTY_STRING;

      if (wpfPath.endsWith(Constant.xmlExtention)) {
        v3File = wpfPath.substring(0, wpfPath.length() - Constant.xmlExtention.length()) + Constant.v3extention + Constant.xmlExtention;
      }
      else
      {
        v3File = wpfPath + Constant.v3extention;
      }

      this.api.getWorkPackage().setWorkpackage(this.documentV3);

      GeneralValidator.printToFile(this.documentV3, v3File);
      GeneralValidator.printToFile(this.documentV3, this.api.getWpfPath());

      GeneralValidator.validateFile(this.api, this.output, new String[] { Constant.modFolder + File.separator + Constant.standardMod, Constant.modFolder + File.separator + Constant.ajcp3Mod });
    }
    catch (Exception e)
    {
      GeneralValidator.logError(this.api, e);
    }
    return this.documentV2V3;
  }

  private void createWorkpackage(Element ajcpPubNodeV3, Element ajcpPubNodeV2V3)
  {
    try
    {
      this.envFile = XPathEx.getString(Constant.AROBASE + Constant.ENV, ajcpPubNodeV2V3);
      this.documentV3.renameNode(ajcpPubNodeV3, Constant.EMPTY_STRING, Constant.AJCPWORKPACKAGE_V3);

      NamedNodeMap nodeMap = ajcpPubNodeV2V3.getAttributes();
      Node users = this.documentV3.createElement(Constant.USERPARAMS_V3);
      for (int i = 0; i < nodeMap.getLength(); i++)
      {
        String localName = nodeMap.item(i).getLocalName();
        String value = nodeMap.item(i).getNodeValue();

        EnvVar attributeEnvVar = new EnvVar(localName, value, this);

        if (attributeEnvVar.getKey() == Constant.CONFIG)
        {
          ajcpPubNodeV3.setAttribute(attributeEnvVar.getKey().toLowerCase(), attributeEnvVar.getValueFromEnvFile());
        }
        else
        {
          Element nodeParam = this.documentV3.createElement(Constant.AJCPCOMMONPARAM_V3);
          nodeParam.setAttribute(Constant.PARAM_TYPE, Constant.PARAM_TYPE_VAL);

          nodeParam.setAttribute(Constant.PARAM_NAME, attributeEnvVar.getKey());
          nodeParam.setAttribute(Constant.PARAM_VALUE, attributeEnvVar.getValueFromEnvFile());
          users.appendChild(nodeParam);
        }

      }

      if (users.hasChildNodes())
      {
        ajcpPubNodeV3.appendChild(users);
      }

      String version = this.env_var_prop.getProperty(Constant.AJCP_VERSION);
      if (version == null)
      {
        version = Constant.VERSION_DEFAULT_VALUE;
      }

      ajcpPubNodeV3.setAttribute(Constant.NODE_V3_VERSION_KEY, version);
      ajcpPubNodeV3.setAttribute(Constant.XMLNS_PRIVATE_KEY, Constant.XMLNS_PRIVATE_VALUE);
      ajcpPubNodeV3.setAttribute(Constant.XMLNS_STANDARD_KEY, Constant.XMLNS_STANDARD_VALUE);
      ajcpPubNodeV3.setAttribute(Constant.XMLNS_URN_KEY, Constant.XMLNS_URN_VALUE);
      ajcpPubNodeV3.setAttribute(Constant.XMLNS_COMMON_KEY, Constant.XMLNS_COMMON_VALUE);

      ajcpPubNodeV3.setAttribute(Constant.XMLNS_AJCP_KEY, Constant.XMLNS_AJCP_VALUE);

      this.listPubli = Publication.createAllPublicationsFromWP(this, EnvVar.removeDollarAndBrackets(this.envFile));
      if (!this.listPubli.isEmpty())
      {
        Element pubsetElement = this.documentV3.createElement(Constant.PUBSET_V3);
        for (int i = 0; i < this.listPubli.size(); i++)
        {
          Publication publication = (Publication)this.listPubli.get(i);
          Element publicationElement = this.documentV3.createElement(Constant.PUBLICATION_V3);
          publicationElement.setAttribute(Constant.PUBLICATION_SERVICE, publication.getService());
          publicationElement.setAttribute(Constant.PUBLICATION_MODEL, publication.getModel());
          publicationElement.setAttribute(Constant.PUBLICATION_CUSTOMER, publication.getCustomer());
          XMLGregorianCalendar revDate = publication.getRevDate();
          if (revDate != null)
          {
            publicationElement.setAttribute(Constant.PUBLICATION_REVDATE, publication.getRevDate().toXMLFormat());
          }

          publicationElement.setAttribute(Constant.PUBLICATION_BASEID, publication.getName());
          pubsetElement.appendChild(publicationElement);
        }
        if (pubsetElement.hasChildNodes())
        {
          ajcpPubNodeV3.appendChild(pubsetElement);
        }

      }

      Element ajcpFormatNodeV2V3 = (Element)XPathEx.getNode(Constant.AJCPFORMAT, ajcpPubNodeV2V3);
      if (ajcpFormatNodeV2V3 != null)
      {
        Element ajcpFomatNodeV3 = this.documentV3.createElement(Constant.AJCPFORMAT_V3);
        createFormat(ajcpFomatNodeV3, ajcpFormatNodeV2V3, ajcpPubNodeV3);
      }
    } catch (Exception e) {
      GeneralValidator.logError(this.api, e);
    }
  }

  private void createFormat(Element ajcpFomatNodeV3, Element ajcpFormatNodeV2V3, Element ajcpPubNodeV3)
  {
    try
    {
      Node additionalInfoV2V3 = XPathEx.getNode(Constant.ADDITIONAL_INFO, ajcpFormatNodeV2V3);
      if (additionalInfoV2V3 != null)
      {
        Node additionalInfoV3 = this.documentV3.importNode(additionalInfoV2V3, true);
        this.documentV3.renameNode(additionalInfoV3, Constant.EMPTY_STRING, Constant.ADDITIONAL_INFO_V3);
        ajcpFomatNodeV3.appendChild(additionalInfoV3);
      }
      NamedNodeMap nodeMapFormat = ajcpFormatNodeV2V3.getAttributes();

      Node usersFormat = this.documentV3.createElement(Constant.USERPARAMS_V3);
      for (int i = 0; i < nodeMapFormat.getLength(); i++)
      {
        String localName = nodeMapFormat.item(i).getLocalName();
        String value = nodeMapFormat.item(i).getNodeValue();

        EnvVar attributeEnvVar = new EnvVar(localName, value, this);

        if (attributeEnvVar.getKey() == Constant.PRINTER)
        {
          ajcpFomatNodeV3.setAttribute(Constant.PRTCONF_V3, attributeEnvVar.getValueFromEnvFile());
        }
        else
        {
          Element nodeParam = this.documentV3.createElement(Constant.AJCPCOMMONPARAM_V3);
          nodeParam.setAttribute(Constant.PARAM_TYPE, Constant.PARAM_TYPE_VAL);

          nodeParam.setAttribute(Constant.PARAM_NAME, attributeEnvVar.getKey());
          nodeParam.setAttribute(Constant.PARAM_VALUE, attributeEnvVar.getValueFromEnvFile());
          usersFormat.appendChild(nodeParam);
        }
      }

      if (usersFormat.hasChildNodes())
      {
        ajcpFomatNodeV3.appendChild(usersFormat);
      }

      Element wpident = this.documentV3.createElement(Constant.WPIDENT_V3);
      Element acident = this.documentV3.createElement(Constant.ACIDENT_V3);
      Element applicabilityFilter = this.documentV3.createElement(Constant.APPLICABILITYFILTER_V3);
      Element applicabilityFilterItem = this.documentV3.createElement(Constant.FILTERITEM_V3);
      Element ajcppub = (Element)XPathEx.getNode(Constant.XPATH_ROOT + Constant.AJCPPUB, this.documentV2V3);
      if (ajcppub.hasAttribute(Constant.PUBID))
      {
        String wpnumberV2V3 = XPathEx.getString(Constant.XPATH_ROOT + Constant.AJCPPUB + Constant.SLASH + Constant.AROBASE + Constant.PUBID, this.documentV2V3);
        Element wpnumber = this.documentV3.createElement(Constant.WPNUMBER_V3);
        wpnumber.setTextContent(wpnumberV2V3);
        wpident.appendChild(wpnumber);
      }
      if (ajcppub.hasAttribute(Constant.FLEET))
      {
        String fleetV2V3 = XPathEx.getString(Constant.XPATH_ROOT + Constant.AJCPPUB + Constant.SLASH + Constant.AROBASE + Constant.FLEET, this.documentV2V3);
        EnvVar attributeEnvVar = new EnvVar(Constant.FLEET, fleetV2V3, this);
        Element fleet = this.documentV3.createElement(Constant.FLEET_V3);
        fleet.setTextContent(attributeEnvVar.getValueFromEnvFile());
        acident.appendChild(fleet);
      }
      applicabilityFilterItem.setAttribute(Constant.CRITERIA_V3, Constant.ALLMSN);
      applicabilityFilter.appendChild(applicabilityFilterItem);

      wpident.appendChild(applicabilityFilter);
      ajcpFomatNodeV3.appendChild(wpident);
      if (acident.hasChildNodes())
      {
        ajcpFomatNodeV3.appendChild(acident);
      }
      ajcpPubNodeV3.appendChild(ajcpFomatNodeV3);

      NodeList ajcpPrintNodeListV2V3 = XPathEx.getNodeList(Constant.AJCPPRINT, ajcpFormatNodeV2V3);

      for (int i = 0; i < ajcpPrintNodeListV2V3.getLength(); i++)
      {
        Element ajcpPrintNodeV2V3 = (Element)ajcpPrintNodeListV2V3.item(i);
        Element ajcpPrintNodeV3 = this.documentV3.createElement(Constant.AJCPPRINT_V3);
        createPrint(ajcpPrintNodeV3, ajcpPrintNodeV2V3, ajcpFomatNodeV3);
      }
    } catch (Exception e) {
      GeneralValidator.logError(this.api, e);
    }
  }

  private void createPrint(Element ajcpPrintNodeV3, Element ajcpPrintNodeV2V3, Element ajcpFormatNodeV3)
  {
    try
    {
      NamedNodeMap nodeMapFormat = ajcpPrintNodeV2V3.getAttributes();
      Node usersFormat = this.documentV3.createElement(Constant.USERPARAMS_V3);
      for (int i = 0; i < nodeMapFormat.getLength(); i++)
      {
        String localName = nodeMapFormat.item(i).getLocalName();
        String value = nodeMapFormat.item(i).getNodeValue();

        EnvVar attributeEnvVar = new EnvVar(localName, value, this);

        if ((attributeEnvVar.getKey().equals(Constant.EID)) || (attributeEnvVar.getKey().equals(Constant.TAGID)) || (attributeEnvVar.getKey().equals(Constant.ATTACHBEFORE)) || (attributeEnvVar.getKey().equals(Constant.ATTACHAFTER)) || (attributeEnvVar.getKey().equals(Constant.CARDNAME)))
        {
          if (localName.equals(Constant.EID))
          {
            ajcpPrintNodeV3.setAttribute(Constant.EID_V3, attributeEnvVar.getValueFromEnvFile());
          }
          else
          {
            ajcpPrintNodeV3.setAttribute(attributeEnvVar.getKey().toLowerCase(), attributeEnvVar.getValueFromEnvFile());
          }
        }
        else
        {
          Element nodeParam = this.documentV3.createElement(Constant.AJCPCOMMONPARAM_V3);
          nodeParam.setAttribute(Constant.PARAM_TYPE, Constant.PARAM_TYPE_VAL);

          nodeParam.setAttribute(Constant.PARAM_NAME, attributeEnvVar.getKey());
          nodeParam.setAttribute(Constant.PARAM_VALUE, attributeEnvVar.getValueFromEnvFile());
          usersFormat.appendChild(nodeParam);
        }
      }
      if (usersFormat.hasChildNodes())
      {
        ajcpPrintNodeV3.appendChild(usersFormat);
      }

      Element jobCardV2V3 = (Element)XPathEx.getNode(Constant.JOBCARD, ajcpPrintNodeV2V3);
      if (jobCardV2V3 != null)
      {
        Element jobCardV3 = (Element)this.documentV3.importNode(jobCardV2V3, true);
        this.documentV3.renameNode(jobCardV3, Constant.EMPTY_STRING, Constant.JOBCARD_V3);
        XPathEx.renameAllNodes(this.documentV3, Constant.CUSTOMERINFO, Constant.CUSTOMERINFO_V3, jobCardV3);
        ajcpPrintNodeV3.appendChild(jobCardV3);
      }

      Element summaryV2V3 = (Element)XPathEx.getNode(Constant.SUMMARY, ajcpPrintNodeV2V3);
      if (summaryV2V3 != null)
      {
        Element summaryV3 = (Element)this.documentV3.importNode(summaryV2V3, true);
        this.documentV3.renameNode(summaryV3, Constant.EMPTY_STRING, Constant.SUMMARY_V3);
        ajcpPrintNodeV3.appendChild(summaryV3);
      }

      Element debutWPV2V3 = (Element)XPathEx.getNode(Constant.DEBUTWP, ajcpPrintNodeV2V3);
      if (debutWPV2V3 != null)
      {
        Element debutWPV3 = (Element)this.documentV3.importNode(debutWPV2V3, true);
        this.documentV3.renameNode(debutWPV3, Constant.EMPTY_STRING, Constant.STARTWP_V3);
        ajcpPrintNodeV3.appendChild(debutWPV3);
      }

      Element finWPV2V3 = (Element)XPathEx.getNode(Constant.FINWP, ajcpPrintNodeV2V3);
      if (finWPV2V3 != null)
      {
        Element finWPV3 = (Element)this.documentV3.importNode(finWPV2V3, true);
        this.documentV3.renameNode(finWPV3, Constant.EMPTY_STRING, Constant.ENDWP_V3);
        ajcpPrintNodeV3.appendChild(finWPV3);
      }

      Element attachmentV2V3 = (Element)XPathEx.getNode(Constant.ATTACHMENT, ajcpPrintNodeV2V3);
      if (attachmentV2V3 != null)
      {
        Element attachmentV3 = (Element)this.documentV3.importNode(attachmentV2V3, true);
        this.documentV3.renameNode(attachmentV3, Constant.EMPTY_STRING, Constant.ATTACHMENT_V3);

        NodeList attachgrList = XPathEx.getNodeList(Constant.ATTACHGR, attachmentV3);
        for (int i = 0; i < attachgrList.getLength(); i++)
        {
          Element attachGr = (Element)attachgrList.item(i);
          XPathEx.changeCaseAttributesNames(0, attachGr);

          attachGr.removeAttribute(Constant.ATTACHGR_JCNUMBER);
          attachGr.removeAttribute(Constant.ATTACHGR_ACTYPE);
          attachGr.removeAttribute(Constant.ATTACHGR_MSREF);
          attachGr.removeAttribute(Constant.ATTACHGR_EFFECT);
        }

        NodeList attachList = XPathEx.getNodeList(Constant.ATTACH, attachmentV3);
        for (int i = 0; i < attachList.getLength(); i++)
        {
          Element attach = (Element)attachList.item(i);
          XPathEx.changeCaseAttributesNames(0, attach);
        }

        NodeList ajcpIndexList = XPathEx.getNodeList(Constant.XPATH_ROOT + Constant.AJCPINDEX, attachmentV3);

        for (int ajcpindexIndex = 0; ajcpindexIndex < ajcpIndexList.getLength(); ajcpindexIndex++) {
          Element ajcpIndex = (Element)ajcpIndexList.item(ajcpindexIndex);
          XPathEx.setAttributeIfValid(ajcpIndex, Constant.TAGID_V3, Constant.TAGID, ajcpIndex);
          ajcpIndex.removeAttribute(Constant.TAGID);
          String envBase = XPathEx.getString(Constant.AROBASE + Constant.BASE, ajcpIndex);
          Publication publi = Publication.getPublicationFromGroupByBase(this.listPubli, EnvVar.removeDollarAndBrackets(envBase));
          if (publi != null)
          {
            ajcpIndex.setAttribute(Constant.PUBID_V3, publi.getName());
          }
          else
          {
            ajcpIndex.setAttribute(Constant.PUBID_V3, envBase);
            this.api.logReport(MessageLevel.INFORMATION, "A publication cannot be found in the properties file : EnvFile = " + EnvVar.removeDollarAndBrackets(this.envFile) + " EnvBase = " + EnvVar.removeDollarAndBrackets(envBase));
          }

          ajcpIndex.removeAttribute(Constant.BASE);
          ajcpIndex.setAttribute(Constant.FORM_V3, Constant.AMMTASKBYATAZONE);
          ajcpIndex.removeAttribute(Constant.TABLE);
          XPathEx.setAttributeIfValid(ajcpIndex, Constant.PARAMS_V3, Constant.CRITERIA, ajcpIndex);

          ajcpIndex.removeAttribute(Constant.CRITERIA);

          constructApplicabilityFilter(ajcpIndex);
        }

        NodeList ajcpIncludeList = XPathEx.getNodeList(Constant.XPATH_ROOT + Constant.AJCPINCLUDE, attachmentV3);

        for (int ajcpIncludeIndex = 0; ajcpIncludeIndex < ajcpIncludeList.getLength(); ajcpIncludeIndex++) {
          Element ajcpInclude = (Element)ajcpIncludeList.item(ajcpIncludeIndex);
          XPathEx.setAttributeIfValid(ajcpInclude, Constant.CONTEXT_V3, Constant.CONTEXT, ajcpInclude);
          ajcpInclude.removeAttribute(Constant.CONTEXT);
          XPathEx.setAttributeIfValid(ajcpInclude, Constant.TAGID_V3, Constant.TAGID, ajcpInclude);
          ajcpInclude.removeAttribute(Constant.TAGID);
          String envBase = XPathEx.getString(Constant.AROBASE + Constant.BASE, ajcpInclude);
          Publication publi = Publication.getPublicationFromGroupByBase(this.listPubli, EnvVar.removeDollarAndBrackets(envBase));
          if (publi != null)
          {
            ajcpInclude.setAttribute(Constant.PUBID_V3, publi.getName());
          }
          else
          {
            ajcpInclude.setAttribute(Constant.PUBID_V3, envBase);
            this.api.logReport(MessageLevel.INFORMATION, "A publication cannot be found in the properties file : EnvFile = " + EnvVar.removeDollarAndBrackets(this.envFile) + " EnvBase = " + EnvVar.removeDollarAndBrackets(envBase));
          }
          if ((ajcpInclude.getAttributes().getNamedItem(Constant.DOC) != null) && (ajcpInclude.getAttributes().getNamedItem(Constant.TARGET) != null))
          {
            String doc = ajcpInclude.getAttribute(Constant.DOC);
            String target = ajcpInclude.getAttribute(Constant.TARGET);
            String targetPath = Constant.DOCAMM + Constant.SLASH + doc + Constant.XPATH_ROOT + Constant.DEBKEY + target + Constant.FINKEY;
            ajcpInclude.setAttribute(Constant.TARGETPATH_V3, targetPath);
            ajcpInclude.removeAttribute(Constant.DOC);
            ajcpInclude.removeAttribute(Constant.TARGET);
          }

          ajcpInclude.removeAttribute(Constant.BASE);
          ajcpInclude.removeAttribute(Constant.CRITERIA);
          ajcpInclude.removeAttribute(Constant.DBREF);
          constructApplicabilityFilter(ajcpInclude);
        }
        XPathEx.renameAllNodes(this.documentV3, Constant.AJCPINDEX, Constant.SEARCH_V3, attachmentV3);
        XPathEx.renameAllNodes(this.documentV3, Constant.AJCPINCLUDE, Constant.SEARCH_V3, attachmentV3);
        XPathEx.renameAllNodes(this.documentV3, Constant.ATTACHGR, Constant.ATTACHGR_V3, attachmentV3);
        XPathEx.renameAllNodes(this.documentV3, Constant.ATTACH, Constant.ATTACH_V3, attachmentV3);
        if (attachmentV3.hasChildNodes())
        {
          ajcpPrintNodeV3.appendChild(attachmentV3);
        }
      }
      ajcpFormatNodeV3.appendChild(ajcpPrintNodeV3);
    } catch (Exception e) {
      GeneralValidator.logError(this.api, e);
    }
  }

  private void constructApplicabilityFilter(Element nodeV2)
  {
    if (nodeV2.hasAttribute(Constant.EFFECT))
    {
      Element applicabilityFilterNode = this.documentV3.createElement(Constant.APPLICABILITYFILTER_V3);
      String effect = nodeV2.getAttribute(Constant.EFFECT);
      if (effect.isEmpty())
      {
        Element filterItemNode = this.documentV3.createElement(Constant.FILTERITEM_V3);
        filterItemNode.setAttribute(Constant.CRITERIA_V3, Constant.ALLFSN);
        applicabilityFilterNode.appendChild(filterItemNode);
        nodeV2.appendChild(applicabilityFilterNode);
      }
      else
      {
        String[] listsApplicability = effect.split(Constant.COMMA);
        ArrayList listItems = new ArrayList();
        for (int index = 0; index < listsApplicability.length; index++)
        {
          String oneApplicability = listsApplicability[index];
          if (oneApplicability.contains(Constant.DASH))
          {
            String[] range = oneApplicability.split(Constant.DASH);
            if (range.length != 2)
            {
              this.api.logReport(MessageLevel.WARNING, "An applicability range is not valid in : " + effect);
            }
            else {
              try
              {
                int min = Math.min(Integer.parseInt(range[0]), Integer.parseInt(range[1]));
                int max = Math.max(Integer.parseInt(range[0]), Integer.parseInt(range[1]));
                for (int currentFilter = min; currentFilter <= max; currentFilter++)
                {
                  listItems.add(Constant.EMPTY_STRING + currentFilter);
                }
              }
              catch (NumberFormatException e)
              {
                this.api.logReport(MessageLevel.WARNING, "An applicability range is not valid : cannot convert " + range[0] + " or " + range[1] + " to Integer");
              }
              catch (Exception e)
              {
                GeneralValidator.logError(this.api, e);
              }
            }
          }
          else
          {
            listItems.add(Constant.EMPTY_STRING + oneApplicability);
          }
        }

        for (int indexSimpleApp = 0; indexSimpleApp < listItems.size(); indexSimpleApp++)
        {
          Element filterItemNode = this.documentV3.createElement(Constant.FILTERITEM_V3);
          String item = (String)listItems.get(indexSimpleApp);
          String attributeValue = Constant.EMPTY_STRING;
          try
          {
            int intemIntValue = Integer.parseInt(item);
            attributeValue = String.format(Constant.FORMAT_3DIGITS, new Object[] { Integer.valueOf(intemIntValue) });
          }
          catch (NumberFormatException e)
          {
            attributeValue = item;
            this.api.logReport(MessageLevel.WARNING, "A value in applicability is not a number : " + item);
          }
          filterItemNode.setAttribute(Constant.CRITERIA_V3, Constant.FSN + attributeValue);
          applicabilityFilterNode.appendChild(filterItemNode);
          nodeV2.appendChild(applicabilityFilterNode);
        }
      }
      nodeV2.removeAttribute(Constant.EFFECT);
    }
  }

  private void renameNode(String oldName, String nameSpace, String newName)
  {
    try
    {
      NodeList list = this.documentV2V3.getElementsByTagName(oldName);

      for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        this.documentV2V3.renameNode(n, nameSpace, newName);
      }
    } catch (Exception e) {
      GeneralValidator.logError(this.api, e);
    }
  }

  public TriggerApi getApi() {
    return this.api;
  }

  public Properties getEnv_var_prop() {
    return this.env_var_prop;
  }

  public Properties getPublication_prop() {
    return this.publication_prop;
  }
}